package com.huc.service.impl;

import com.huc.entity.UserDO;
import com.huc.mapper.UserMapper;
import com.huc.query.UserQuery;
import com.huc.service.UserService;
import com.huc.tools.IdWorkerTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDO getUserByName(UserQuery query) {
        return userMapper.getUserByName(query);
    }

    @Override
    public String adUser(UserDO userDO) {
        IdWorkerTools worker = new IdWorkerTools(1, 1, 1);
        Long userId = worker.nextId();
        userDO.setUserId(String.valueOf(userId));
        userDO.setAddTime(new Date());
        String tableName = "user" + (Math.abs(String.valueOf(userId).hashCode() / 2 % 1) + 1);
        userDO.setTableName(tableName);
        System.out.println("表节点：" + tableName);

        userMapper.insert(userDO);
        return userId + "";
    }
}
